﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MentorBackEnd.Model;
using System.Data.SqlClient;
using System.Data;

namespace MentorBackEnd.DA
{
    public class UserDA
    {
        public static void CreateNewUser(MemaUser user)
        {
            SqlConnection conn = DataAccessor.GetConnection();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MemaUserInsert]"); 
                cmd.Parameters.Add(new SqlParameter("@UserTypeID", (int) user.UserType));
                cmd.Parameters.Add(new SqlParameter("@FirstName", user.FirstName));
                cmd.Parameters.Add(new SqlParameter("@LastName", user.LastName));
                cmd.Parameters.Add(new SqlParameter("@Email", user.Email));
                cmd.Parameters.Add(new SqlParameter("@Password",  user.Password));
                cmd.Parameters.Add(new SqlParameter("@IsActive", user.IsActive));

                cmd.ExecuteNonQuery();
            }

            catch { throw; }
            finally { conn.Close(); }
            return;
        }

        public static MemaUser Authenticate(string email, string password)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();
             
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MemaUserAuth]");
                cmd.Parameters.Add(new SqlParameter("@Email", email));
                cmd.Parameters.Add(new SqlParameter("@Password", password));


                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    MemaUser dataModel = new MemaUser(reader);
                    collection.Add(dataModel);
                   
                }

                if (!reader.IsClosed)
                    reader.Close();

                if (collection.Count > 0)
                    return collection[0];
                else
                    return null;
            }

            catch { throw; }
            finally { conn.Close(); }
            
        }

        public static List<MemaUser> SelectUser(int userID)
        {
            SqlConnection conn = DataAccessor.GetConnection();
            IDataReader reader = null;
            List<MemaUser> collection = new List<MemaUser>();

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = ("[or_MemaUserSelect]");
                cmd.Parameters.Add(new SqlParameter("@MeMaUserID", userID)); 
                cmd.ExecuteNonQuery();
            
                 reader = cmd.ExecuteReader();
                 while (reader.Read())
                 {
                     MemaUser dataModel = new MemaUser(reader);
                     collection.Add(dataModel);
                 }

                 if (!reader.IsClosed)
                     reader.Close();

                 return collection;
            }
            catch { throw; }
            finally { conn.Close(); }
             
        }
    }
}
